package com.zhongyouyun.admin.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongyouyun.admin.model.DictionaryDetailedModel;
import com.zhongyouyun.admin.model.DictionaryModel;
import com.zhongyouyun.admin.model.UserModel;
import com.zhongyouyun.admin.service.DictionaryService;
import com.zhongyouyun.other.annotation.SysLogAnnotation;
import com.zhongyouyun.other.model.PageRequest;
import com.zhongyouyun.other.model.Result;
import com.zhongyouyun.utils.ModelUtils;
import com.zhongyouyun.utils.QueryUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhongyouyun.admin.service.DictionaryDetailedService;

import javax.management.Query;
import java.util.ArrayList;
import java.util.Date;
import java.util.Dictionary;
import java.util.List;


/**
 * @Author: shenyi
 * @Date: 2023-02-01
 * @Description:
 */
@RestController
@RequestMapping("/admin/z_dictionary_detailed")
@Api(value = "基础字典管理详情管理", tags = {"基础字典详情管理接口"})
public class DictionaryDetailedController {
    @Autowired
    private DictionaryDetailedService service;

    @Autowired
    private DictionaryService dictionaryService;

    @GetMapping("/findList")
    @ResponseBody
    @ApiOperation(value = "查询字典详情列表", notes = "通过条件查询字典详情列表列表")
    @SysLogAnnotation(operModul = "系统管理>>字典详情", operType = "字典详情列表", operDesc = "字典详情列表")
    public List<DictionaryDetailedModel> findList(@ApiParam(name = "字典ID", value = "id", required = true) @RequestParam(value = "id") String id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("dictionary_id", id);
        return service.selectList(queryWrapper);
    }

    @GetMapping("/findNameList")
    @ResponseBody
    @ApiOperation(value = "查询所属字典详情列表", notes = "通过字典key查询字典详情列表列表")
    @SysLogAnnotation(operModul = "系统管理>>字典详情", operType = "字典详情列表", operDesc = "字典详情列表")
    public List<DictionaryDetailedModel> findNameList(@ApiParam(name = "字典key", value = "fieldName", required = true) @RequestParam(value = "fieldName") String fieldName) {
        DictionaryModel dictionaryModel = dictionaryService.getOne(new QueryWrapper<DictionaryModel>().eq("field_name", fieldName));
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("dictionary_id", dictionaryModel.getId());
        return service.selectList(queryWrapper);
    }

    @PostMapping("/saveOrUpate")
    @ResponseBody
    @ApiOperation(value = "修改或者新增", notes = "通过ID判断修改或者新增")
    public Result saveOrUpate(@RequestBody DictionaryDetailedModel dictionaryDetailedModel) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("dictionary_key", dictionaryDetailedModel.getDictionaryKey());
        UserModel userModel = (UserModel) SecurityUtils.getSubject().getPrincipal(); // 获取当前登录用户
        if (ObjectUtil.isNotEmpty(dictionaryDetailedModel.getId())) {
            dictionaryDetailedModel.setCreateTime(new Date());
            queryWrapper.notIn("id", dictionaryDetailedModel.getId());
        } else {
            dictionaryDetailedModel.setUpdateTime(new Date());
            // ModelUtils modelutils = new ModelUtils();
            // modelutils.initModel(dictionaryDetailedModel, userModel.getId().toString());
        }


        int count = service.count(queryWrapper);
        if (count > 0) {

            return Result.builder().message("字典键不能重复").success(false).status(1).build();
        } else {
            Boolean flag = service.saveOrUpdate(dictionaryDetailedModel);
            if (flag) {
                return Result.builder().message("成功").status(0).build();
            } else {
                return Result.builder().message("保存失败").success(false).status(1).build();
            }
        }

    }

    @GetMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "删除", notes = "通过ID删除")
    public Result delete(@ApiParam(name = "字典详情ID", value = "id", required = true) @RequestParam(value = "id") String id) {
        Boolean flag = false;
        flag = service.removeById(Long.valueOf(id));
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("删除失败").success(false).status(1).build();
        }
    }

    @PostMapping("/batchDelete")
    @ResponseBody
    @ApiOperation(value = "批量删除", notes = "通过ID删除")
    public Result batchDelete(@RequestBody List<String> ids) {
        List<Long> list = new ArrayList<Long>();
        ids.forEach(id -> {
            list.add(Long.valueOf(id));
        });
        Boolean flag = service.removeByIds(list);
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("失败").success(false).status(1).build();
        }
    }

}
